<template>
  <el-dialog
    title="修改品牌"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      size="mini"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="dataForm.name"
          disabled
          placeholder="分类名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="品牌名称" prop="brandIds">
        <el-select
          multiple
          filterable
          v-model="dataForm.brandIds"
          style="width: 100%"
        >
          <el-option
            v-for="item in brandArrays"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addOrUpdateCategoryBrand,
  findProductBrandByCategoryId,
  productBrandList,
} from "@/api/product";

export default {
  data() {
    return {
      brandArrays: [],
      addOrUpdateVisible: false,
      visible: false,
      dataListLoading: false,
      dataForm: {
        id: 0,
        brandIds: [],
        name: "",
      },
      dataRule: {
        name: [{ required: true, message: "组名不能为空", trigger: "blur" }],
      },
    };
  },
  components: {},

  activated() {},
  methods: {
    productBrandList() {
      productBrandList().then((res) => {
        if (res && res.code === 0) {
          this.brandArrays = res.data;
        }
      });
    },
    findProductBrandByCategoryId() {
      findProductBrandByCategoryId({ categoryId: this.dataForm.id }).then(
        (res) => {
          if (res && res.code === 0) {
            this.dataForm.brandIds = [].concat(res.data).map((item) => {
              return item.id;
            });
          }
        }
      );
    },
    init(data) {
      this.dataForm.id = data.id;
      this.dataForm.name = data.name;
      this.visible = true;
      this.productBrandList();
      this.$nextTick(() => {
        this.findProductBrandByCategoryId();
        this.$refs["dataForm"].resetFields();
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          addOrUpdateCategoryBrand({
            categoryId: this.dataForm.id || "",
            brandIds: this.dataForm.brandIds,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
